package com.iknow.task;

import android.app.ProgressDialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.RemoteException;
import com.iknow.IKnow;
import com.iknow.IKnowApiV4;
import com.iknow.OnlineAction;
import com.iknow.User;
import com.iknow.data.QingBo;
import com.iknow.data.QingboType;
import com.iknow.data.Word;
import com.iknow.db.IKProductListDataInfo;
import com.iknow.db.IKnowDataBase;
import com.iknow.db.IKnowDatabaseHelper;
import com.iknow.http.HttpException;
import com.iknow.ui.model.FavoriteQingboAdapter;
import com.iknow.ui.model.QingBoAdapter;
import com.iknow.util.Loger;
import com.iknow.util.StringUtil;
import com.iknow.xmpp.service.Friend;
import com.iknow.xmpp.service.aidl.IKnowUser;
import com.iknow.xmpp.service.aidl.IXmppConnection;
import com.iknow.xmpp.service.aidl.IXmppFacade;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.List;

public class CommonTask
{
  public static class BingSessionTask extends GenericTask
  {
    protected TaskResult _doInBackground(TaskParams[] paramArrayOfTaskParams)
    {
      return TaskResult.FAILED;
    }
  }

  public static class DataMigration extends AsyncTask<Void, Integer, Void>
  {
    protected FavoriteQingboAdapter mAdapter;
    public Context mContext = null;
    protected ProgressDialog pd;

    public DataMigration(Context paramContext, FavoriteQingboAdapter paramFavoriteQingboAdapter)
    {
      this.mAdapter = null;
      this.pd = null;
      this.mContext = paramContext;
      this.mAdapter = paramFavoriteQingboAdapter;
      this.pd = new ProgressDialog(paramContext);
    }

    protected Void doInBackground(Void[] paramArrayOfVoid)
    {
      List localList = IKnow.mIKnowDataBase.getProduct();
      int i = localList.size();
      int j = 100 / i;
      int k = j;
      int l = 0;
      while (true)
      {
        if (l >= i)
          return null;
        IKProductListDataInfo localIKProductListDataInfo = (IKProductListDataInfo)localList.get(l);
        String str1 = localIKProductListDataInfo.getId();
        String str2 = localIKProductListDataInfo.getUserId();
        QingBo localQingBo = new QingBo(str1, localIKProductListDataInfo.getBookName(), "", new Date(localIKProductListDataInfo.getProductFavoriteTime()), "", "", str2);
        localQingBo.setQingboType(QingboType.text);
        IKnow.mIKnowDataBase.addFavoriteQingbo(localQingBo);
        Integer[] arrayOfInteger = new Integer[1];
        arrayOfInteger[0] = Integer.valueOf(k);
        publishProgress(arrayOfInteger);
        k += j;
        ++l;
      }
    }

    protected void onCancelled()
    {
      super.onCancelled();
      this.pd.setProgress(0);
    }

    protected void onPostExecute(Void paramVoid)
    {
      super.onPostExecute(paramVoid);
      Integer[] arrayOfInteger = new Integer[1];
      arrayOfInteger[0] = Integer.valueOf(100);
      publishProgress(arrayOfInteger);
      List localList = IKnow.mIKnowDataBase.getFavoriteQingbo(0, 999);
      this.mAdapter.setQingBoList(localList);
      this.mAdapter.notifyDataSetChanged();
      this.pd.dismiss();
      IKnowDatabaseHelper.getDatabase(this.mContext).execSQL("DROP TABLE IF EXISTS T_BD_PRODUCTFAVORITES");
    }

    protected void onPreExecute()
    {
      super.onPreExecute();
      this.pd.setProgressStyle(1);
      this.pd.setTitle("提示");
      this.pd.setMessage("正在导入本地文章收藏，请稍后");
      this.pd.setProgress(0);
      this.pd.setCancelable(false);
      this.pd.show();
    }

    protected void onProgressUpdate(Integer[] paramArrayOfInteger)
    {
      super.onProgressUpdate(paramArrayOfInteger);
      this.pd.setProgress(paramArrayOfInteger[0].intValue());
    }
  }

  public static class DownloadFileTask extends GenericTask
  {
    private String mErrorMessage;

    protected TaskResult _doInBackground(TaskParams[] paramArrayOfTaskParams)
    {
      String str1;
      String str2;
      TaskParams localTaskParams = paramArrayOfTaskParams[0];
      try
      {
        str1 = localTaskParams.getString("url");
        str2 = localTaskParams.getString("file_name");
      }
      catch (Exception localException1)
      {
        TaskResult localTaskResult;
        try
        {
          URL localURL = new URL(str1);
          URLConnection localURLConnection = localURL.openConnection();
          localURLConnection.connect();
          int i = localURLConnection.getContentLength();
          BufferedInputStream localBufferedInputStream = new BufferedInputStream(localURL.openStream());
          FileOutputStream localFileOutputStream = new FileOutputStream(str2);
          byte[] arrayOfByte = new byte[1024];
          long l = 0L;
          int j = localBufferedInputStream.read(arrayOfByte);
          if (j == -1)
          {
            localFileOutputStream.flush();
            localFileOutputStream.close();
            localBufferedInputStream.close();
            localTaskResult = TaskResult.OK;
            while (true)
            {
              return localTaskResult;
              localException1 = localException1;
              localException1.printStackTrace();
              localTaskResult = TaskResult.FAILED;
            }
          }
          l += j;
          Object[] arrayOfObject = new Object[1];
          arrayOfObject[0] = Integer.valueOf((int)(100L * l / i));
          publishProgress(arrayOfObject);
          localFileOutputStream.write(arrayOfByte, 0, j);
        }
        catch (Exception localException2)
        {
          while (true)
          {
            localException2.printStackTrace();
            this.mErrorMessage = "网络超时，稍后再试";
            localTaskResult = TaskResult.FAILED;
          }
        }
      }
    }

    public String getErrorMessage()
    {
      return this.mErrorMessage;
    }
  }

  public static class GetInfoTask extends GenericTask
  {
    private String msg = null;

    protected TaskResult _doInBackground(TaskParams[] paramArrayOfTaskParams)
    {
      TaskResult localTaskResult;
      TaskParams localTaskParams1 = paramArrayOfTaskParams[0];
      try
      {
        String str = localTaskParams1.getString("jid");
        Friend localFriend = ((IXmppFacade)localTaskParams1.get("xmpp")).createConnection().getIKnowUser().getUserInfo(str);
        if (localFriend != null)
        {
          TaskParams localTaskParams2 = new TaskParams();
          localTaskParams2.put("friend", localFriend);
          Object[] arrayOfObject = new Object[1];
          arrayOfObject[0] = localTaskParams2;
          publishProgress(arrayOfObject);
          localTaskResult = TaskResult.OK;
        }
        else
        {
          localTaskResult = TaskResult.FAILED;
        }
      }
      catch (HttpException localHttpException)
      {
        Loger.e("GetInfoTask", localHttpException.getMessage());
        localHttpException.printStackTrace();
        this.msg = localHttpException.getMessage();
        localTaskResult = TaskResult.FAILED;
      }
      catch (RemoteException localRemoteException)
      {
        while (true)
        {
          this.msg = localRemoteException.getMessage();
          localRemoteException.printStackTrace();
        }
      }
      return localTaskResult;
    }

    public String getMsg()
    {
      return this.msg;
    }
  }

  public static class ProductFavoriteTask extends GenericTask
  {
    public static final int ACTION_ADD = 1;
    public static final int ACTION_BUCKUP = 5;
    public static final int ACTION_DELETE = 2;
    public static final int ACTION_READ_LOCAL = 0;
    public static final int ACTION_RESOTRE = 4;
    public static final int ACTION_SELECT = 3;
    private Context ctx;
    private List<QingBo> list = null;
    private int mActionCode;
    private String mErrorMessage;
    private IXmppFacade mXmppFacade;

    public ProductFavoriteTask(Context paramContext)
    {
      this.ctx = paramContext;
    }

    protected TaskResult _doInBackground(TaskParams[] paramArrayOfTaskParams)
    {
      TaskResult localTaskResult;
      TaskParams localTaskParams = paramArrayOfTaskParams[0];
      try
      {
        this.mActionCode = localTaskParams.getInt("action");
        this.mXmppFacade = ((IXmppFacade)localTaskParams.get("xmpp"));
        if (!(isLogin()))
        {
          localTaskResult = TaskResult.FAILED;
        }
        else
        {
          int i = this.mActionCode;
          switch (i)
          {
          case 3:
          default:
            localTaskResult = TaskResult.OK;
            break;
          case 0:
            List localList3 = IKnow.mIKnowDataBase.getFavoriteQingbo(0, 999);
            Object[] arrayOfObject = new Object[1];
            arrayOfObject[0] = localList3;
            label345: publishProgress(arrayOfObject);
          case 1:
          case 2:
          case 4:
          case 5:
          }
        }
      }
      catch (HttpException localHttpException)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                this.mErrorMessage = "网络超时,稍后再试";
                Loger.e("ProductFavoriteTask", this.mErrorMessage);
                localHttpException.printStackTrace();
                localTaskResult = TaskResult.FAILED;
                break label345:
                if (!(authContentUser()))
                {
                  localTaskResult = TaskResult.FAILED;
                  break label345:
                }
                IKnow.mApi.doFavorite(OnlineAction.Add, getQBList());
              }
              if (!(authContentUser()))
              {
                localTaskResult = TaskResult.FAILED;
                break label345:
              }
              IKnow.mApi.doFavorite(OnlineAction.Delete, getQBList());
            }
            if (!(authContentUser()))
            {
              localTaskResult = TaskResult.FAILED;
              break label345:
            }
            String str = IKnow.mIKnowDataBase.getUser().getUID();
            if (StringUtil.isEmpty(str))
            {
              this.mErrorMessage = "找不到用户";
              localTaskResult = TaskResult.FAILED;
              break label345:
            }
            List localList2 = IKnow.mApi.getFavoriteQingbos(str, 0, 999);
            IKnowDatabaseHelper.getDatabase(this.ctx).execSQL("DELETE FROM favorite");
            IKnow.mIKnowDataBase.addFavoriteQingbo(localList2);
          }
          if (!(authContentUser()))
          {
            localTaskResult = TaskResult.FAILED;
            break;
          }
          List localList1 = IKnow.mIKnowDataBase.getFavoriteQingbo(0, 999);
          IKnow.mApi.doFavorite(OnlineAction.Add, localList1);
        }
        return localTaskResult;
      }
    }

    public boolean authContentUser()
    {
      int i;
      try
      {
        if ((this.mXmppFacade == null) || (this.mXmppFacade.createConnection() == null) || (!(this.mXmppFacade.createConnection().authenticatContentUser())))
        {
          this.mErrorMessage = "网络超时，稍候再试";
          i = 0;
          return i;
        }
      }
      catch (RemoteException localRemoteException)
      {
        while (true)
        {
          localRemoteException.printStackTrace();
          i = 0;
        }
        i = 1;
      }
    }

    public int getActionCode()
    {
      return this.mActionCode;
    }

    public String getErrorMsg()
    {
      return this.mErrorMessage;
    }

    public List<QingBo> getQBList()
    {
      return this.list;
    }

    public boolean isLogin()
    {
      int i;
      if (!(((IKnow)this.ctx.getApplicationContext()).isConnected()))
      {
        this.mErrorMessage = "网络超时，稍后再试";
        i = 0;
      }
      while (true)
      {
        return i;
        i = 1;
      }
    }

    public void setQBList(List<QingBo> paramList)
    {
      this.list = paramList;
    }
  }

  public static class QingboOperateTask extends GenericTask
  {
    public static int ACTION_DELETE = 1;

    protected TaskResult _doInBackground(TaskParams[] paramArrayOfTaskParams)
    {
      TaskResult localTaskResult;
      TaskParams localTaskParams = paramArrayOfTaskParams[0];
      try
      {
        int i = localTaskParams.getInt("code");
        String str = localTaskParams.getString("id");
        if (i == ACTION_DELETE)
          IKnow.mApi.deleteQingbo(str);
        localTaskResult = TaskResult.OK;
        return localTaskResult;
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
        localTaskResult = TaskResult.FAILED;
      }
    }
  }

  public static class QingboTask extends GenericTask
  {
    private QingBoAdapter mAdapter;
    private String mErrorMessage;

    protected TaskResult _doInBackground(TaskParams[] paramArrayOfTaskParams)
    {
      TaskResult localTaskResult;
      TaskParams localTaskParams = paramArrayOfTaskParams[0];
      String str1 = null;
      String str2 = null;
      String str3 = null;
      String str4 = null;
      int i = 0;
      int j = 0;
      try
      {
        if (localTaskParams.has("user_id"))
          str1 = localTaskParams.getString("user_id");
        if (localTaskParams.has("offset"))
          i = localTaskParams.getInt("offset");
        if (localTaskParams.has("length"))
          j = localTaskParams.getInt("length");
        if (localTaskParams.has("tags"))
          str2 = localTaskParams.getString("tags");
        if (localTaskParams.has("create_user"))
          str3 = localTaskParams.getString("create_user");
        if (localTaskParams.has("keyword"))
          str4 = localTaskParams.getString("keyword");
        if (str1 != null)
          localObject = IKnow.mApi.getQingboListByFollow(str1, i, j);
        while (true)
        {
          while (true)
          {
            while (true)
            {
              if (localObject != null)
              {
                Object[] arrayOfObject = new Object[1];
                arrayOfObject[0] = localObject;
                publishProgress(arrayOfObject);
              }
              localTaskResult = TaskResult.OK;
              break label273:
              if (str3 == null)
                break;
              localObject = IKnow.mApi.getQingboListByRosterID(str3, i, j);
            }
            if (str2 == null)
              break;
            localObject = IKnow.mApi.getQingboListByTag(str2, i, j);
          }
          if (str4 == null)
            break;
          localObject = IKnow.mApi.queryQingboListByKeyword(str4, i, j);
        }
        List localList = IKnow.mApi.getQingboList(i, j);
        label273: Object localObject = localList;
      }
      catch (HttpException localHttpException)
      {
        this.mErrorMessage = "网络超时,稍后再试";
        localHttpException.printStackTrace();
        localTaskResult = TaskResult.FAILED;
        return ((TaskResult)localTaskResult);
      }
    }

    public String getErrorMessage()
    {
      return this.mErrorMessage;
    }

    public void setAdapter(QingBoAdapter paramQingBoAdapter)
    {
      this.mAdapter = paramQingBoAdapter;
    }
  }

  public static class WordTask extends GenericTask
  {
    public static final int ACTION_ADD = 1;
    public static final int ACTION_BUCKUP = 5;
    public static final int ACTION_DELETE = 2;
    public static final int ACTION_READ_LOCAL = 0;
    public static final int ACTION_RESOTRE = 4;
    public static final int ACTION_SELECT = 3;
    private Context ctx;
    public int mActionCode;
    private String mErrorMessage;
    private List<Word> wordList;

    public WordTask(Context paramContext)
    {
      this.ctx = paramContext;
    }

    protected TaskResult _doInBackground(TaskParams[] paramArrayOfTaskParams)
    {
      TaskResult localTaskResult;
      TaskParams localTaskParams = paramArrayOfTaskParams[0];
      try
      {
        this.mActionCode = localTaskParams.getInt("action");
        if ((!(isLogin())) && (getActionCode() != 0))
        {
          localTaskResult = TaskResult.FAILED;
        }
        else
        {
          int i = this.mActionCode;
          switch (i)
          {
          case 3:
          default:
            localTaskResult = TaskResult.OK;
            break;
          case 0:
            Object[] arrayOfObject = new Object[1];
            arrayOfObject[0] = this.wordList;
            label261: publishProgress(arrayOfObject);
          case 1:
          case 2:
          case 5:
          case 4:
          }
        }
      }
      catch (HttpException localHttpException)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                this.mErrorMessage = "网络超时，稍后再试";
                Loger.e("WordFavoriteTask", localHttpException.getMessage());
                localHttpException.printStackTrace();
                localTaskResult = TaskResult.FAILED;
                break label261:
                IKnow.mApi.doWordFavorite(OnlineAction.Add, getWordList());
              }
              IKnow.mApi.doWordFavorite(OnlineAction.Delete, getWordList());
            }
            IKnow.mApi.doWordFavorite(OnlineAction.Add, getWordList());
          }
          String str = IKnow.mIKnowDataBase.getUser().getUID();
          if (StringUtil.isEmpty(str))
          {
            this.mErrorMessage = "找不到用户";
            localTaskResult = TaskResult.FAILED;
            break;
          }
          List localList = IKnow.mApi.getFavoriteWord(str, 0, 999);
          IKnowDatabaseHelper.getDatabase(this.ctx).execSQL("DELETE FROM T_BD_STRANGEWORD");
          IKnow.mIKnowDataBase.addWord(localList);
        }
        return localTaskResult;
      }
    }

    public int getActionCode()
    {
      return this.mActionCode;
    }

    public String getMsg()
    {
      return this.mErrorMessage;
    }

    public List<Word> getWordList()
    {
      return this.wordList;
    }

    public boolean isLogin()
    {
      int i;
      if (!(((IKnow)this.ctx.getApplicationContext()).isConnected()))
      {
        this.mErrorMessage = "网络超时，稍后再试";
        i = 0;
      }
      while (true)
      {
        return i;
        i = 1;
      }
    }

    public void setActionCode(int paramInt)
    {
      this.mActionCode = paramInt;
    }

    public void setWordList(List<Word> paramList)
    {
      this.wordList = paramList;
    }
  }
}